/**
 * @file 管理并缓存monaco文件
 */
import _ from 'lodash'

const initialContent = {}

const getUri = _.memoize(file => new window.monaco.Uri().with({
  path: file,
  scheme: 'file'
}))

const modelManager = {
  getModel (filePath, content, noCreate, lang) {
    if (!window.monaco) return null
    const uri = getUri(filePath)
    let model = window.monaco.editor.getModel(uri)
    if (!model && !noCreate) {
      model = window.monaco.editor.createModel(content || initialContent[filePath], lang, uri)
      model.updateOptions({tabSize: 2})
    }
    return model
  },
  getInitialValue (filePath) {
    return initialContent[filePath] || ''
  },
  reset (filePath) {
    if (!filePath) return
    const model = this.getModel(filePath, null, true)
    if (model && model.getValue() !== this.getInitialValue(filePath)) {
      model.setValue(this.getInitialValue(filePath) || '')
    }
  },
  setValue (filePath, content) {
    const model = this.getModel(filePath)
    if (model && model.getValue() !== content) {
      model.setValue(content)
    }
  },
  setInitialValue (filePath, content) {
    if (initialContent[filePath] === content) return
    const model = this.getModel(filePath, content, true)
    if (model && (!_.has(initialContent, filePath) || model.getValue() === initialContent[filePath])) {
      model.setValue(content)
    }
    initialContent[filePath] = content
  },
  getValue (filePath) {
    const model = this.getModel(filePath)
    if (model) return model.getValue()
  },
  hasModel (filePath) {
    return !!this.getModel(filePath, null, true)
  },
  isChanged (filePath) {
    return filePath && _.has(initialContent, filePath) && this.hasModel(filePath) && initialContent[filePath] !== this.getValue(filePath)
  },
  save (filePath) {
    initialContent[filePath] = this.getValue(filePath)
  }
}

export default modelManager
